using Concurso;
using Concurso.Clases;
using Curriculum.Modelo.Clases;
using Utiles;

namespace ConcursoCV.UI.WebUserControls.Administrador
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using Curriculum.Services;
	using System.Collections;

	/// <summary>
	///		Summary description for ABMAdministradores.
	/// </summary>
	public class ABMAdministradores : System.Web.UI.UserControl
	{
		#region Atributos
		protected System.Web.UI.WebControls.TextBox txtApellido;
		protected System.Web.UI.WebControls.TextBox txtNombre;
		protected System.Web.UI.WebControls.Button btnBuscar;
		protected System.Web.UI.WebControls.Button btnLimpiar;
		protected System.Web.UI.WebControls.DataGrid dtgAdministradores;
		protected System.Web.UI.WebControls.Label lblCount;
		protected System.Web.UI.WebControls.Button btnNuevo;

		private const string pidABMAdministrador = "022";
		#endregion Atributos

		#region Metodos
		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!this.IsPostBack)
			{				
				InitializeVariables();								
			}
		}

		private void InitializeVariables()
		{
			SortColumn = "Apellido";
			SortDirection = true;			
		}

		private void FillSearchVariables() 
		{
			FiltroApellido = txtApellido.Text.Trim() != string.Empty ? txtApellido.Text.Trim() : string.Empty;
			FiltroNombre = txtNombre.Text.Trim() != string.Empty ? txtNombre.Text.Trim() : string.Empty;			
		}

		private IList GetListBySearch() 
		{
			AdministradorService service = new AdministradorService();
			return service.GetAdministradores(FiltroApellido,FiltroNombre);
		}

		private void FillDataGridWithSearch()
		{
			IList list = GetListBySearch();
			((ArrayList)list).Sort(new GenericComparer(SortColumn,SortDirection));
			dtgAdministradores.DataSource = list;
			lblCount.Text = list.Count.ToString();
			try
			{
				dtgAdministradores.DataBind();
			}
			catch(Exception exc)
			{
				string msg = exc.Message;
				dtgAdministradores.CurrentPageIndex = 0;
			}
		}

		private void RedirectToAdministradorABM(PageMode mode, string administradorId)
		{
			string url = "Default.aspx?pid=" + pidABMAdministrador + "&administradorId=" + administradorId + "&PageMode=" + PageModeConverter.ToInt( mode );
			Server.Transfer(url);
		}

		private void EliminarAdministrador(string administradorId)
		{
			AdministradorService svc = new AdministradorService();
			svc.EliminarAdministrador(administradorId);
		}

		#endregion Metodos
		
		#region Properties 
		public string FiltroNombre
		{
			get{ return (string)ViewState["_filtroNombre"];}
			set{ ViewState["_filtroNombre"] = value;}
		}
		public string FiltroApellido
		{
			get{ return (string)ViewState["_filtroApellido"];}
			set{ ViewState["_filtroApellido"] = value;}
		}
		
		public string SortColumn
		{
			get{ return (string)ViewState["SortColumn"];}
			set{ ViewState["SortColumn"] = value;}
		}
		public bool SortDirection
		{
			get{ return (bool)ViewState["SortDirection"];}
			set{ ViewState["SortDirection"] = value;}
		}

		#endregion Properties

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnBuscar.Click += new System.EventHandler(this.btnBuscar_Click);
			this.btnLimpiar.Click += new System.EventHandler(this.btnLimpiar_Click);
			this.dtgAdministradores.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dtgAdministrador_ItemCommand);
			this.dtgAdministradores.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dtgAdministradores_PageIndexChanged);
			this.dtgAdministradores.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dtgAdministrador_SortCommand);
			this.btnNuevo.Click += new System.EventHandler(this.btnNuevo_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region Eventos
		private void btnBuscar_Click(object sender, System.EventArgs e)
		{
			dtgAdministradores.CurrentPageIndex=0;
			FillSearchVariables();
			FillDataGridWithSearch();
		}

		private void btnLimpiar_Click(object sender, System.EventArgs e)
		{
			txtApellido.Text = string.Empty;
			txtNombre.Text = string.Empty;			
		}

		private void btnNuevo_Click(object sender, System.EventArgs e)
		{
			string url = "Default.aspx?pid=" + pidABMAdministrador;
			Server.Transfer(url);
		}

		private void dtgAdministrador_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if ( e.CommandName != "Page" ) 
			{
				string administradorId = e.Item.Cells[0].Text;				
				switch ( e.CommandName ) 
				{
					case "View":
						RedirectToAdministradorABM(PageMode.VIEW,administradorId);
						break;

					case "Modify":
						RedirectToAdministradorABM(PageMode.EDIT,administradorId);
						break;
						
					case "Delete":
						EliminarAdministrador(administradorId);	
						FillDataGridWithSearch();
						break;
				}
			}			
		}

		private void dtgAdministrador_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			//Si se hace click sobre ,la misma columna, cambia el orden
			if (SortColumn.ToString() == e.SortExpression.ToString())
				SortDirection = !SortDirection;
			
			SortColumn = e.SortExpression;
			FillDataGridWithSearch();				
		}

		private void dtgAdministradores_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			dtgAdministradores.CurrentPageIndex = e.NewPageIndex;
			FillDataGridWithSearch();
		
		}

		#endregion Eventos
		

	}
}
